package test.temple;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.weight.common.generate.GenerateImp;
import com.weight.common.generate.base.ResourceFile;
import com.weight.common.util.CommonUtils;


/**
* 
*  
*/
public class MapperSQLResourceFileNew extends ResourceFile {
	
	private static final String FILE_NAME = ".sql";
	public MapperSQLResourceFileNew(String filePath,String packageName,String model,String tableName
			,String tableCnName, List<String> fieldDOList, List<String> fieldENList, 
			List<String> fieldTypeList,List<String> fieldSizeList,List<String> fieldIndexList,List<String> fieldCNList) {
		super(filePath,new StringBuilder().append(packageName).append(FILE_NAME).toString());
		
		StringBuilder  resourceFile =new StringBuilder();
		//1.删表建表
		resourceFile.append("DROP TABLE IF EXISTS `").append(tableName).append("` ;\n");
		resourceFile.append("\n");
		resourceFile.append("CREATE TABLE IF NOT EXISTS `").append(tableName).append("` (\n");
		//常量表跟关系表自动生成ID
		if(tableName.endsWith("_ral")||tableName.endsWith("_norm")) {
			resourceFile.append("\t`").append(tableName).append("_id`\tINT(8)\tNOT\tNULL\tAUTO_INCREMENT\tCOMMENT\t'")
			.append(tableCnName).append("Id',\n");
		}//userid int(4) primary key not null auto_increment,
		//循环生成字段
		for(int i=0;i<fieldDOList.size();i++) {			
			if(StringUtils.equalsIgnoreCase(fieldIndexList.get(i), "primary")) {
				resourceFile.append("\t`").append(fieldDOList.get(i).toLowerCase()).append("`\t")
				.append(fieldTypeList.get(i).toUpperCase()).append("(")
				.append(fieldSizeList.get(i)).append(")")
				.append("\tNOT\tNULL\tCOMMENT\t'").append(fieldCNList.get(i))
				.append("',\n");
			}else if(fieldSizeList.get(i)!=null) {
				resourceFile.append("\t`").append(fieldDOList.get(i).toLowerCase().trim()).append("`\t")
				.append(fieldTypeList.get(i).toUpperCase()).append("(")
				.append(fieldSizeList.get(i)).append(")")
				.append("\tNULL\tCOMMENT\t'").append(fieldCNList.get(i))
				.append("',\n");
			}else {
				try {
					resourceFile.append("\t`").append(fieldDOList.get(i).toLowerCase().trim()).append("`\t")
					.append(fieldTypeList.get(i).toUpperCase()).append("\tNULL\tCOMMENT\t'").append(fieldCNList.get(i))
					.append("',\n");
				} catch (Exception e) {
					System.out.println(e.getMessage());
					System.out.println(fieldDOList.get(i));
					
				}
			}
		}
		
		//添加索引键
		int kk=0;
		//信息表添加固定字段
		if(tableName.endsWith("_info")||tableName.endsWith("_log")) {
			resourceFile.append("\t`create_time`\tTIMESTAMP\tNOT\tNULL\tDEFAULT\tCURRENT_TIMESTAMP\tCOMMENT\t'创建时间',\n");
			resourceFile.append("\t`update_time`\tTIMESTAMP\tNOT\tNULL\tDEFAULT\tCURRENT_TIMESTAMP\tON\tUPDATE\tCURRENT_TIMESTAMP\tCOMMENT\t'修改时间',\n");
			resourceFile.append("\t`create_by`\tVARCHAR(32)\tNOT\tNULL\tCOMMENT\t'创建人',\n");
			resourceFile.append("\t`update_by`\tVARCHAR(32)\tNOT\tNULL\tCOMMENT\t'修改人',\n");
			resourceFile.append("\t`standbyfield1`\tVARCHAR(512)\tNULL\tDEFAULT\t'0'\tCOMMENT\t'备用字段1',\n");
			resourceFile.append("\t`standbyfield2`\tVARCHAR(512)\tNULL\tDEFAULT\t'0'\tCOMMENT\t'备用字段2',\n");
			resourceFile.append("\t`standbyfield3`\tVARCHAR(512)\tNULL\tDEFAULT\t'0'\tCOMMENT\t'备用字段3',\n");
		}else {//常量表关系表
//		if(tableName.endsWith("_ral")||tableName.endsWith("_norm")) {
			resourceFile.append("\tPRIMARY KEY (`").append(tableName).append("_id`)");
			kk++;
		}
		
		for(int i=0;i<fieldDOList.size();i++) {
			if(fieldIndexList.get(i)!=null) {
				if(fieldIndexList.get(i).equalsIgnoreCase("primary")) {
					if(kk>0) {
						resourceFile.append(",\n");
					}
					resourceFile.append("\tPRIMARY KEY (`").append(fieldDOList.get(i).toLowerCase().trim()).append("`)");
					kk++;
				}else if(fieldIndexList.get(i).equalsIgnoreCase("index")) {
					if(kk>0) {
						resourceFile.append(",\n");
					}
					resourceFile.append("\tKEY `idx_").append(fieldDOList.get(i).toLowerCase().trim()).append("` (`")
					.append(fieldDOList.get(i).toLowerCase().trim()).append("`)");
					kk++;
				}else if(fieldIndexList.get(i).equalsIgnoreCase("UNIQUE")) {
					if(kk>0) {
						resourceFile.append(",\n");
					}
					resourceFile.append("\tUNIQUE KEY `uk_").append(fieldDOList.get(i).toLowerCase().trim()).append("` (`")
					.append(fieldDOList.get(i).toLowerCase().trim()).append("`)");
					kk++;
				}
				
			}			
		}
		
		resourceFile.append("\n)COMMENT = '").append(tableCnName).append("';\n");
		resourceFile.append("\n");
		
		
		 
		resourceFile.append("\n");
		
		String tableId = CommonUtils.getUUId();
		
		for(int i=0;i<fieldDOList.size();i++) {
			resourceFile.append("insert	into	datagroupd_info\n" + 
					"(data_id,datatab_id,datatab_ename,datatab_cname,data_ename,data_cname,data_type,data_store,data_show,\n" + 
					"data_limit,data_status,create_by,update_by,standbyfield1,standbyfield2,standbyfield3)\n" + 
					"values\n" + 
					"('"+CommonUtils.getUUId()+"','"+tableId+"','"+getModelClass(model).toString()+"','"+tableCnName+"'\n,'"
					+fieldENList.get(i).trim()+"','"+fieldCNList.get(i)
					+"',1,'','',0, 1,'00','00','','','');\n");
			
			resourceFile.append("\n");
		}
		
		String resource = resourceFile.toString();
		init(resource);
	}
}
